package com.sondon.mayi.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.entity.GoodInfo;
import com.sondon.mayi.entity.GoodInfoEntity;
import com.sondon.mayi.entity.RespondBaseEntity;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.ui.MyMultipleTopBar;
import com.sondon.mayi.ui.TopBarClickListener;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.GsonUtil;
import com.sondon.mayi.util.ImageLoaderUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.MyAlertDialog;
import com.sondon.mayi.util.ShareUtil;
import com.sondon.mayi.util.ToastUtils;

/**
 * @Project : Fruit
 * @Package : com.sondon.mayi.activity
 * @Class : GoodInfoActivity
 * @Author : 蔡文锋
 * @DateTime：2015年6月10日 下午10:34:52
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : {商品介绍}
 *
 */
@EActivity(R.layout.good_info)
public class GoodInfoActivity extends BaseActivity {
	@ViewById
	MyMultipleTopBar topBar;
	@ViewById
	ImageView wide_pic;// 商品图片
	@ViewById
	TextView name;// 商品名称
	@ViewById
	LinearLayout share_box;// 分享
	@ViewById
	TextView market_price;// 市场价
	@ViewById
	TextView user_price;// 用户价
	@ViewById
	TextView price_comment;// 折价
	@ViewById
	ImageView contact_kf;// 联系卖家
	@ViewById
	ImageView buy;// 加入购物车
	@ViewById
	LinearLayout upgrade_box;// 更低价格购买
	@ViewById
	LinearLayout comments;// 评论
	@ViewById
	TextView friends_buy_count;// 朋友购买
	@ViewById
	TextView comments_count;// 商品评价
	@ViewById
	TextView description;// 商品介绍
	@ViewById
	LinearLayout pic_text;// 图文详情 图文详情URL
	@ViewById
	LinearLayout photoes;// 商品实拍  商品实拍URL

	private String good_id;//物品ID
	
	@Click({ R.id.share_box, R.id.contact_kf, R.id.buy, R.id.upgrade_box,
			R.id.comments, R.id.pic_text, R.id.photoes })
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.share_box://分享
			ShareUtil.ShareSDK(context);
			break;
		case R.id.contact_kf://联系卖家
            //用intent启动拨打电话  
            Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+myprefs.custom_phone().get()));  
            startActivity(intent);  
			break;
		case R.id.buy://加入购物车
			Add2Cart();
			break;
		case R.id.upgrade_box://更低价格购买
			ToastUtils.show(context, "更低价格购买....");
			break;
		case R.id.comments://评论
			ToastUtils.show(context, "评论....");
			break;
		case R.id.pic_text:// 图文详情
			Bundle bundle=new Bundle();
			bundle.putString("url", String.valueOf(pic_text.getTag()));
			bundle.putString("title", "图文详情");
			openActivity(WebViewActivity_.class,bundle);
			break;
		case R.id.photoes://商品实拍
			Bundle bundle2=new Bundle();
			bundle2.putString("url", String.valueOf(photoes.getTag()));
			bundle2.putString("title", "商品实拍");
			openActivity(WebViewActivity_.class,bundle2);
			break;
		default:
			break;
		}
	}

	@AfterViews
	public void initView() {
			topBar.setTopBarClickListener(new TopBarClickListener() {

			@Override
			public void rightBtnClick() {

			}

			@Override
			public void leftBtnClick() {
				onBackPressed();
			}
		});
		
		//获取商品详情信息
		good_id=getIntent().getExtras().getString("good_id");
		AsyncHttpUtil.get(URLConfigs.GoodsInfo_ukey+good_id+"&ukey="+myprefs.Ukey().get(), null, GoodInfoRes);
	}

	
	/*
	 * 加入购物车
	 */
	public  void Add2Cart(){
		//判断是否已经登录
		if("".equals(myprefs.Ukey().get())){
			ToastUtils.show(context, "请登录");
			return;
		}
		
		RequestParams params=new RequestParams();
		params.put("goods_id", this.good_id);
		params.put("num", 1);
		AsyncHttpUtil.post(URLConfigs.Add2Cart_ukey+myprefs.Ukey().get(), params, Add2CartRes);
	}
	
	
	
	//加入购物车响应
	TextHttpResponseHandler Add2CartRes= new TextHttpResponseHandler() {
		
		@Override
		public void onSuccess(int status, Header[] arg1, String json) {
			if(status==200){
				LogUtil.e("GoodsList_Item", "Add2Cart_ukey  json  :"+json);
				RespondBaseEntity rsbEntity=GsonUtil.GetFromJson(json, RespondBaseEntity.class);
//				ToastUtils.show(context, rsbEntity.getMsg());
				if(rsbEntity.isStatus()){//成功
					MyAlertDialog.show(context, "购物车", "商品已成功加入购物车", "继续购物","去购物车");
					//左边按钮监听事件
					OnClickListener	 leftListener=new OnClickListener() {
										
										@Override
										public void onClick(View arg0) {
											if(MyAlertDialog.getMydialog().isShowing()){
												MyAlertDialog.getMydialog().dismiss();
											}
										}
									};
					//右边按钮监听事件
					OnClickListener	 rightListener=new OnClickListener() {
										
										@Override
										public void onClick(View arg0) {
											if(MyAlertDialog.getMydialog().isShowing()){
												MyAlertDialog.getMydialog().dismiss();
												Intent  intent=new Intent(context,CartActivity_.class);
												context.startActivity(intent);
											}
										}
									};
					//设置监听事件
					MyAlertDialog.setLeftRightListener(leftListener, rightListener);
				
//					LayoutInflater inflaterDl = LayoutInflater.from(context);
//					  LinearLayout  myview = (LinearLayout)inflaterDl.inflate(R.layout.my_alertdialog, null );
//			         //对话框
//			         final Dialog dialog = new AlertDialog.Builder(context).create();
//			         dialog.show();
//			         dialog.getWindow().setContentView(myview);
//			         
//			         //继续购物
//			         myview.findViewById(R.id.continue_shopping).setOnClickListener(new OnClickListener() {
//						
//						@Override
//						public void onClick(View arg0) {
//							if(dialog.isShowing()){
//								dialog.dismiss();
//							}
//						}
//					});
//			         
//			        //去购物车 
//			         myview.findViewById(R.id.go2cart).setOnClickListener(new OnClickListener() {
//							
//							@Override
//							public void onClick(View arg0) {
//									dialog.dismiss();
//									Intent  intent=new Intent(context,CartActivity_.class);
//									context.startActivity(intent);
//							}
//						});
				}else{
					//请求错误
					ToastUtils.show(context, rsbEntity.getMsg());
				}
			}else{
				ToastUtils.show(context, R.string.network_unavailable);
			}
		}
		
		@Override
		public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
			ToastUtils.show(context, R.string.network_unavailable);
		}
	};
	
	
	//商品详情响应
	TextHttpResponseHandler GoodInfoRes=new TextHttpResponseHandler() {
		
		@Override
		public void onSuccess(int status, Header[] arg1, String json) {
			LogUtil.e(TAG, "GoodInfo json :"+json);
			if(status==200){
				GoodInfoEntity entity=GsonUtil.GetFromJson(json, GoodInfoEntity.class);
				if(entity.isStatus()){
					GoodInfo goodinfo=entity.getInfo();
					ImageLoaderUtil.SetImgView(goodinfo.getImg(), wide_pic);
					name.setText(goodinfo.getName());
					market_price.setText(goodinfo.getMarket_price());
					user_price.setText(goodinfo.getGroup_price());
					
					description.setText(goodinfo.getDescription());
					friends_buy_count.setText(goodinfo.getFriends_buy());
					comments_count.setText(goodinfo.getFriends_buy());
					pic_text.setTag(goodinfo.getDes_url());
					photoes.setTag(goodinfo.getPicdes_url());
					
				}else{
					ToastUtils.show(context, entity.getMsg());
				}
			}else{
				ToastUtils.show(context, R.string.network_unavailable);
			}
		}
		
		@Override
		public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
			
			ToastUtils.show(context, R.string.network_unavailable);
		}
	};
}
